<template>
    <div class="purchase">

        <h3 style="text-align: center;font-size: large;margin-bottom: 20px;">货物采购</h3>
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
            <Form-item label="货物编号" prop="goods_num">
                <Input icon="ios-grid" v-model="formValidate.goods_num" placeholder="请输入货物编号" number></Input>
            </Form-item>
            <Form-item label="货物名称" prop="goods_name">
                <Input icon="ios-paper" v-model="formValidate.goods_name" placeholder="请输入货物名称"></Input>
            </Form-item>
            <Form-item label="类别" prop="category">
                <Select v-model="formValidate.category" placeholder="请选择类别">
                    <Option value="文具">文具</Option>
                    <Option value="家用电器">家用电器</Option>
                    <Option value="食品">食品</Option>
                </Select>
            </Form-item>

            <Form-item label="进货数量" prop="purchase_quantity">
                <Input icon="ios-calculator" v-model="formValidate.purchase_quantity" placeholder="请输入采购数量"
                    number></Input>
            </Form-item>

            <Form-item label="进货价格" prop="purchase_price">
                <Input icon="logo-yen" v-model="formValidate.purchase_price" placeholder="请输采购货物单价"></Input>
            </Form-item>
            <Form-item label="供应厂商" prop="purchase_supplier">
                <Input icon="ios-home" v-model="formValidate.purchase_supplier" placeholder="请输入货物供应厂商"></Input>
            </Form-item>
            <Form-item label="负责人" prop="purchase_responsible">
                <Input icon="md-person" v-model="formValidate.purchase_responsible" placeholder="请输入进货负责人"></Input>
            </Form-item>
            <Form-item>
                <i-col span="12">
                    <Button type="primary" @click="handleSubmit('formValidate',formValidate.goods_num,formValidate.goods_name,
                         formValidate.category,formValidate.purchase_quantity,formValidate.purchase_price,
                         formValidate.purchase_supplier,formValidate.purchase_responsible)">提交订单</Button>
                </i-col>
                <i-col span="12">
                    <Button type="success" @click="handleReset('formValidate')" style="margin-left: 8px">重置订单</Button>
                </i-col>

            </Form-item>
        </Form>

    </div>
</template>

<script>
    import api from '../network/api/inventory.js'
    export default {
        name: 'purchase_add',

        data() {
            const validateNumber = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('选项不能为空或0'));
                }
                // 模拟异步验证效果
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 1) {
                            callback(new Error('不能小于1'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                formValidate: {
                    goods_num: '',
                    goods_name: '',
                    category: '',
                    purchase_quantity: '',
                    purchase_price: '',
                    purchase_supplier: '',
                    purchase_responsible: ''

                },
                ruleValidate: {
                    goods_num: [
                        { validator: validateNumber, required: true, trigger: 'blur' }
                    ],
                    goods_name: [
                        { required: true, message: '货物名称不能为空', trigger: 'blur' },
                    ],
                    category: [
                        { required: true, message: '货物类型不能为空', trigger: 'blur' }
                    ],
                    purchase_quantity: [
                        { validator: validateNumber, required: true, trigger: 'blur' }
                    ],
                    purchase_price: [
                        { required: true, message: '进货价格不能为空', trigger: 'blur' },
                    ],
                    purchase_supplier: [
                        { required: true, message: '进货厂商名称不能为空', trigger: 'blur' }
                    ],
                    purchase_responsible: [
                        { required: true, message: '负责人不能为空', trigger: 'blur' }
                    ]
                },
            };
        },

        mounted() {

        },

        methods: {
            handleSubmit(name, goods_num, goods_name, category, purchase_quantity, purchase_price, purchase_supplier, purchase_responsible) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let params = {
                            goods_num: goods_num,
                            goods_name: goods_name,
                            category: category,
                            purchase_quantity: purchase_quantity,
                            purchase_price: purchase_price,
                            purchase_supplier: purchase_supplier,
                            purchase_responsible: purchase_responsible
                        };

                        api.purchase_add(params);

                        this.$Message.success('提交成功!');
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset(name) {
                this.$refs[name].resetFields();
            },
        },
    };
</script>

<style lang="scss" scoped>

</style>